/*
 * Copyright (C) 2014-2016 Qiujuer <qiujuer@live.cn>
 * WebSite http://www.qiujuer.net
 * Author qiujuer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui;

import ohos.agp.components.AttrHelper;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * This is Genius UI Center
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public class Ui {
    public static final int COMPLEX_UNIT_DIP = 1;
    public static final int TOUCH_PRESS_COLOR = 0x30000000; //black_alpha_48

    public static final int KEY_SHADOW_COLOR = 0x1F000000; //0x1E000000;
    public static final int FILL_SHADOW_COLOR = 0x3D000000; //0x3D000000;
    public static final float X_OFFSET = 0f;
    public static final float Y_OFFSET = 1.8f;
    public static final float SHADOW_RADIUS = 3.75f;
    public static final int SHADOW_ELEVATION = 4;

    /**
     * Modulate the colorAlpha to new alpha
     *
     * @param colorAlpha Color's alpha
     * @param alpha Modulate alpha
     * @return Modulate alpha
     */
    public static int modulateAlpha(int colorAlpha, int alpha) {
        int scale = alpha + (alpha >>> 7);  // convert to 0..256
        return colorAlpha * scale >>> 8;
    }

    /**
     * Modulate the color to new alpha
     *
     * @param color Color
     * @param alpha Modulate alpha
     * @return Modulate alpha color
     */
    public static int modulateColorAlpha(int color, int alpha) {
        int colorAlpha = color >>> 24;
        int scale = alpha + (alpha >> 7);
        int newAlpha = colorAlpha * scale >> 8;
        int r = (color >> 16) & 0xFF;
        int g = (color >> 8) & 0xFF;
        int b = color & 0xFF;
        return newAlpha << 24 | r << 16 | g << 8 | b;
    }


    /**
     * Change the color to new alpha
     *
     * @param color Color
     * @param alpha New alpha
     * @return New alpha color
     */
    public static int changeColorAlpha(int color, int alpha) {
        int r = (color >> 16) & 0xFF;
        int g = (color >> 8) & 0xFF;
        int b = color & 0xFF;
        return alpha << 24 | r << 16 | g << 8 | b;
    }


    /**
     * 获取转换值
     *
     * @param unit 转换类型
     * @param value 转换值
     * @param display 屏幕参数
     * @return float float
     */
    public static float dipToPx(int unit, float value, DisplayAttributes display) {
        switch (unit) {
            case COMPLEX_UNIT_DIP:
                return value * display.densityPixels;
        }
        return 0;
    }

    /**
     * 获取屏幕数据
     *
     * @param context 上下文
     * @return 实例
     */
    public static DisplayAttributes getDisplayAttributes(Context context) {
        return DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
    }

    public static int getDimensionPixelOffset(Context context, int resId) {
        try {
            int a = context.getResourceManager().getElement(resId).getInteger();
            return AttrHelper.vp2px(a, AttrHelper.getDensity(context));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
